from .trigger import ObjectEnterTrigger


# 联动进入报警器
class GatherTrigger(ObjectEnterTrigger):
    
    def __init__(self, alert_objs, gather_alert_threshhold):
        super().__init__(alert_objs)
        self._gather_alert_threshhold = gather_alert_threshhold
        self._alerted = {label: False for label in self.alert_objs}

    def enter(self, obj, objs):
        for label in self.alert_objs:
            if len(objs[label]) >= self._gather_alert_threshhold:
                if not self._alerted[label]:
                    # 绘图，给他们全标出来
                    image = obj.image.copy()
                    for o in objs[label]:
                        o.make_shotcut(image)
                    # 报警
                    self.alert_to_cloud({
                        'type': obj.label,
                        'target': f'{obj.label}聚集',
                        'action': 1,
                        'image': {
                            'base64': self.encode_jpg_image(image),
                            'ext': 'jpg'
                        }
                    })
                    self._alerted[label] = True  # 聚集报警一次即可
            else:
                self._alerted[label] = False  # 物体数量低于阈值就允许重新报警
